package com.example.recyclerviewlearn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class ThreadLearn extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "ThreadLearn";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_learn);
        findViewById(R.id.testAsyncTask).setOnClickListener(this);
        findViewById(R.id.handlerThread).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.testAsyncTask) {
            // 使用execute任务默认是顺序执行的
//            new MyAsyncTask("task1").execute();
//            new MyAsyncTask("task2").execute();
//            new MyAsyncTask("task3").execute();
//            new MyAsyncTask("task4").execute();
//            new MyAsyncTask("task5").execute();
//            new MyAsyncTask("task6").execute();
//            new MyAsyncTask("task7").execute();
//            new MyAsyncTask("task8").execute();

            // 使用executeOnExecutor指定在固定的executors上执行任务
            new MyAsyncTask("task1").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 1");
            new MyAsyncTask("task2").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 2");
            new MyAsyncTask("task3").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 3");
            new MyAsyncTask("task4").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 4");
            new MyAsyncTask("task5").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 5");
            new MyAsyncTask("task6").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 6");
            new MyAsyncTask("task7").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 7");
            new MyAsyncTask("task8").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "liuxudong 8");
        } else if (v.getId() == R.id.handlerThread) {

        }
    }

    /**
     * Async默认是串行执行的，我们可以通过 使用executeOnExecutor可以达到并行处理的目的
     */
    private static class MyAsyncTask extends AsyncTask<String, Integer, String> {
        private static final String TAG = "MyAsyncTask";

        private String mName;

        public MyAsyncTask(String name) {
            mName = name;
        }

        @Override
        protected String doInBackground(String... strings) {
            Log.i(TAG, "doInBackground: strings" + Arrays.toString(strings));
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mName;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Log.i("ThreadLearn", "onPostExecute: result" + result + "  df format is " + df.format(new Date()));
        }
    }
}